#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# @Time: 2023/6/16 11:53
# @Author: Anthony (anthony66888@163.com)
# @description: 
"""
import random
import time
from urllib import parse

import requests

from auto_battle_room import YamlControl


class UserInfo:
    def __init__(self):
        """初始化登陆后台"""
        config = YamlControl("/data/config.yaml").read_yaml()
        self.host = config["admin_host"]  # 生产环境
        self.token = config["admin_token"]  # 后台token
        self.post_headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Token": self.token
        }
        self.get_headers = {
            "Token": self.token
        }

    def get_uid(self, phone):
        """获取用户ID"""
        url = "/api/sys/user/customer/page"
        data = {
            "phone": phone,
            "totalCount": 1,
            "page": 1,
            "pageSize": 10
        }
        response = requests.get(url=self.host + url, params=data, headers=self.get_headers)
        return response.json()["data"]["pageData"][0]["id"]

    def get_user_balance(self, uid):
        """
        获取用户当前余额
        :param uid: 用户ID
        :return: 用户当前余额
        """
        url = "/api/sys/user/customer/userInfo"
        data = {
            "id": uid
        }
        response = requests.get(url=self.host + url, params=data, headers=self.get_headers)
        balance = - response.json()["data"]["balance"]
        return balance

    def add_balance(self, uid):
        """后台单次给用户充值"""
        value = random.choice([random.randint(800, 999),
                               random.randint(1500, 1999),
                               random.randint(2500, 4999),
                               random.randint(5500, 9999)])

        url = "/api/sys/user/customer/modifyLevelAndBalance"
        data = {
            "id": uid,
            # "initBalance": 0.00,
            "balance": value,
            "changeBalance": value,
            "grade": 1,
            "experience": 0
        }
        data = parse.urlencode(data)
        requests.post(url=self.host + url, data=data, headers=self.post_headers)
        # response = requests.post(url=self.host + url, data=data, headers=headers)
        # print(response.text)

    def clear_balance(self, uid):
        """后台单次清空用户余额"""
        balance = self.get_user_balance(uid)
        url = "/api/sys/user/customer/modifyLevelAndBalance"
        data = {
            "id": uid,
            # "initBalance": 0.00,
            "balance": balance,
            "changeBalance": balance,
            "grade": 1,
            "experience": 0
        }
        data = parse.urlencode(data)
        requests.post(url=self.host + url, data=data, headers=self.post_headers)
        # response = requests.post(url=self.host + url, data=data, headers=headers)
        # print(response.text)


def batch_add_balance(phone1, phone2=None):
    """
    后台批量给用户充值
    :param phone1: 第一个用户手机号
    :param phone2: 最后一个手机号
    """
    user_info = UserInfo()
    i = 0
    if phone2 is None:
        print(f"开始给{phone1}充值")
        uid = user_info.get_uid(phone1)
        user_info.clear_balance(uid)
        user_info.add_balance(uid)
        print(f"给{phone1}充值完毕")
    else:
        for phone in range(phone1, phone2+1):
            uid = user_info.get_uid(phone)
            user_info.clear_balance(uid)  # 先清空用户余额
            user_info.add_balance(uid)  # 再添加用户余额
            i += 1
            if i % 10 == 0:
                print(f"已充值{i}个用户")
                time.sleep(1)
        print("全部充值完毕")


if __name__ == '__main__':
    # # 测试环境
    # p1 = 26111111110
    # p2 = 26111111118

    # 生产环境
    p1 = 26111111110
    p2 = 26111111399
    batch_add_balance(p1, p2)

